package tests.entity.testcases;

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.naming.InitialContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import com.ticketbox.entity.User;

public class UserTest {

	InitialContext context;
	private EntityManager em;
	
	@Before
	public void setUp() throws Exception {
		this.context = new InitialContext();
		EntityManagerFactory emf=Persistence.createEntityManagerFactory("TicketboxJPA");
		em=emf.createEntityManager();
		if(em.getTransaction().isActive()){
			em.flush();
		}
	}

	@After
	public void tearDown() throws Exception {
		context.close();
	}

	@Test
	public void testCreate() {
		em.getTransaction().begin();
		User user=new User();
		user.setFirstName("Test1stName");
		user.setFirstName("TestLastName");
		user.setEmail("test@domain.com");
		user.setPassword("RandomPassword");
		user.setUsername("testUsername");
		em.persist(user);
		em.getTransaction().commit();
		User foundUser =  (User) em.createNamedQuery("User.findAll").getResultList().get(0);
		assertTrue(foundUser.getUsername().equals(user.getUsername()));
	}

	@Test
	public void testUpdate() {
		
	}

}
